// pages/list/list.js
const app = getApp()
const time = require('../../utils/util.js')
Page({
     /**
      * 页面的初始数据
      */
     data: {
          list:[],//列表
          id:'',//对应的id
          page: 1,//页数
          header:"",//页面头部
     },

     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function (options) {
          var that = this
          that.setData({
               id: options.id
          })
          // 页面加载
          that.request()
          //页面标题为路由参数
          wx.setNavigationBarTitle({
               title: options.title 
          })
         
     },
     /**
      * 页面上拉触底事件的处理函数
      */
     onReachBottom: function () {
          var that = this
          var page = that.data.page
          page++
          that.setData({
               page: page
          })
          //     分页加载
          that.request()
     },

     /**
      * 用户点击右上角分享
      */
     onShareAppMessage: function (res) {
          var that = this
          if (res.from === 'button') {
               // 来自页面内转发按钮
               //console.log(res.target)
          } 
          return {
               title: that.data.header.title,
               path: '/pages/list/list?id=' + that.data.id + '&title=' + that.data.header.title,
               success: function (res) {
                    //console.log(res)
                    wx.showToast({
                         title: '发送成功',
                         icon: 'none'
                    })
               },
               fail: function (res) {
                    // 转发失败
                    wx.showToast({
                         title: '发送失败',
                         icon: 'none'
                    })
               }
          }

     },
     //   请求
     request: function () {
          var that = this
          var page = that.data.page
          var id = that.data.id
          wx.request({
               url: app.data.apiurl + '/qupu/category/' + id,
               dataType: 'json',
               method: "get",
               data: {
                    pageIndex: page,
                    pageSize: 12
               },
               header: {
                    'content-type': 'application/json' // 默认值
               },
               success: function (res) {
                    //console.log(res.data.data)
                         var reslist = res.data.data
                         var list = that.data.list;
                         if (reslist.qupuList.length == "0") {
                              wx.showToast({
                                   title: '暂无数据,敬请期待！',
                                   icon: 'none',
                                   duration: 2000
                              })
                         } else {
                              for (var i = 0; i < reslist.qupuList.length; i++) {
                                   list.push(reslist.qupuList[i]);
                              }
                              //console.log(list)
                              that.setData({
                                   header: reslist.category,
                                   list: list,
                                   hidden: true
                              })
                         }
               }
          })
     }
})